const Menu = require('@/models/sys/MenuModel')

interface IMenu{
    icon: string,
    url: string,
    name: string,
    menuId: string,
    orderNum: number,
    parentId: string,
    parentName: string
}

export function get () {
    return new Promise((resolve, reject) => {
        Menu.aggregate([
            {
                $match: {'parentId': {$lte: '0'}}
            },
            {
                $lookup:{
                    from: 'menu',
                    localField: 'menuId',
                    foreignField: 'parentId',
                    as: 'list'
                }
            },
            {
                $sort:{"orderNum": 1}
            }
        ], (err: any, menuList: IMenu[]) => {
            if (err) return
            resolve({
                code: 0,
                menuList,
                msg: 'success'
            }) 
        })
    }) 
}

export function getList () {
    return Menu.find({}).then((menuList: IMenu[]) => {
        return menuList
    })
}


export function save () {
    let menu = new Menu({
        icon: 'role',
        url: 'sys/role',
        name: '角色管理',
        menuId: '69',
        orderNum: 5,
        parentId: '1',
        parentName: '系统管理'
    })
    menu.save((err: any) => {
        if (err) {
            return
        }
    })
    return {code: 0, msg: '新增成功'}
}

export function update () {}

export function remove () {}